﻿// ---------------------------------------------------------------------------------------------------------------------
//   Copyright (c) SRT Solutions 2009. All rights reserved.
// ---------------------------------------------------------------------------------------------------------------------

using System;
using System.IO;

using NUnit.Framework;

using SRTSolutions.Elevate.IO;

namespace SRTSolutions.Elevate.Guide
{
    public class IOExtensions
    {
        [Test]
        public void ReadingACString()
        {
            //given a binary reader with a CString of a known length that we want to read
            var stringWithNulls = "just a string with some null terminators.\0\0\0\0\0\0\0\0\0";

            using (var stream = new MemoryStream())
            {
                var writer = new BinaryWriter(stream);
                writer.Write(stringWithNulls.ToCharArray());
                writer.Flush();
                stream.Position = 0;

                //we can read the string without the null terminators like this:
                var reader = new BinaryReader(stream);
                var result = reader.ReadString(stringWithNulls.Length);

                Assert.AreEqual("just a string with some null terminators.", result);
            }
        }

        #region Unit Tests

        [Test]
        public void ReadStringWithCountThisException()
        {
            BinaryReader tmp = null;
            Assert.Throws<ArgumentNullException>(() => tmp.ReadString(10));
        }

        [Test]
        public void ReadStringWithCountArgumentException()
        {
            using(var tmp = new BinaryReader(new MemoryStream()))
            {
                Assert.Throws<ArgumentOutOfRangeException>(()=>tmp.ReadString(-5));
            }
        }

        #endregion
    }
}
